﻿#include <print>
#include <iostream>

#include "zzj.col.hpp"
#include "zzj.menu.hpp"
#include "data.hpp"
#include "file_io.cpp"
#include "calc.cpp"
#include "calc.gmap.cpp"

using namespace menu;
using namespace col;
using namespace std::literals;

int main()
{
	Menu menu{ "迷宫求解" };

	menu.ready();
	menu.statusBarFr();
	menu.printMsg(MsgTy::MESSAGE, "打印迷宫");

	maze::Map map(8, 8);

	if (map.inpFrmFile("map_inp_8.ini")) {
		menu.printMsg(MsgTy::SUCCESS, "读取迷宫成功！");
		menu.statFlag.is_readFile = true;

		// 显示地图
		if (map.display()) {
			menu.printMsg(MsgTy::SUCCESS, "地图显示成功！");
		}
		else {
			menu.printMsg(MsgTy::ERROR, "地图显示错误！");
		}

		// 将地图数据写入文件
		if (map.out2File("map_out_8.ini")) {
			menu.printMsg(MsgTy::SUCCESS, "地图数据已成功写入文件 map_out.ini");
		}
		else {
			menu.printMsg(MsgTy::ERROR, "写入文件失败。");
		}
	}
	else {
		menu.printMsg(MsgTy::ERROR, "读取文件失败。");
	}

	std::cout << "\n";
	menu.statusBar();

	menu.printMsg(MsgTy::MESSAGE, "路径计算：");
	if (auto const res{ map.display() };
		res) {
		menu.printMsg(MsgTy::SUCCESS, "打印迷宫成功！");
		std::print("\n");
		menu.printMsg(MsgTy::NOTE, "最小生成堆:");
		menu.printMsg(MsgTy::MESSAGE, "请输入起点：");
		size_t start, end;
		std::cin >> start;
		menu.printMsg(MsgTy::MESSAGE, "请输入终点：");
		std::cin >> end;

		auto result = map.calcPath(start-1, end-1);
		if (!result.first.empty())
		{
			auto &path = result.first;
			auto score = result.second;
			std::cout << "此图路径最小路径以及分数：" << endl;
			std::cout << "分数：" << score << "\n";
			if (map.displayPath(path));
			if (map.displayPathGraph(path));
			menu.printMsg(MsgTy::SUCCESS, "路径计算成功！");

			std::print("\n");
			menu.printMsg(MsgTy::NOTE, "Dijkstra:");
			menu.printMsg(MsgTy::NORMAL,"计算权重路径：");
			GMap g_map{ map };
			g_map.advProc();
			//g_map.printGraph();

			auto result = g_map.dijkstra(start - 1, end - 1);

			std::println("最优路径(L/C)：");
			for (const auto& vertex : result.first) {
				auto [x, y] = projection_conversion(map.m_width, map.m_height, vertex);
				std::print("({}, {}) ", ++x, ++y);
			}

			std::cout << "\n最小权重： " << result.second << std::endl;
			menu.printMsg(MsgTy::SUCCESS, "路径计算成功！");
			g_map.printPath(result.first);
			g_map.printPath2(result.first);

		}
	}
}
